package Bisectionmethod;

/**
 * @Classname JZ11旋转数组的最小数字
 * @Description TODO
 * @Date 2023/2/24 21:54
 * @Created by xjl
 */
public class JZ11旋转数组的最小数字 {
    /**
     * @description 使用的是二分法来实现
     * @param: numbers
     * @date: 2023/2/24 21:54
     * @return: int
     * @author: xjl
     */
    public int minArray(int[] numbers) {
        int low = 0;
        int high = numbers.length - 1;
        while (low < high) {
            int mid = (low + high) >> 1;
            if (numbers[mid] < numbers[high]) {
                high = mid;
            } else if (numbers[mid] > numbers[high]) {
                low = mid + 1;
            } else {
                high = high - 1;
            }
        }
        return numbers[low];
    }

    public int minArray2(int[] numbers) {
        if (numbers.length < 2) {
            return numbers[0];
        }
        int res = numbers[0];
        for (int i = 0; i < numbers.length - 1; i++) {
            if (numbers[i] > numbers[i + 1]) {
                res = numbers[i + 1];
                break;
            }

        }
        return res;
    }
}
